package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.AssetMapper;
import com.think.teaching.model.Asset;
import com.think.teaching.service.AssetService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 资源 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class AssetServiceImpl extends BaseServiceImpl<AssetMapper, Asset> implements AssetService {

	@Override
	public Asset InsertNewAsset(Asset asset) {
		
		if (this.save(asset)) {
			return asset;
		}
		return null;
	}

	@Override
	public IPage<Asset> ListAssetWithCreator(Integer id, Integer page, Integer count, Boolean withoutDelete) {
		// TODO Auto-generated method stub
		QueryWrapper<Asset> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("created_by", id);
		queryWrapper.ne("state", 0);
		queryWrapper.ne("delflag", withoutDelete ? 1 : 0);
		IPage<Asset> assetPage = new Page<>(page, count);
		return baseMapper.selectPage(assetPage, queryWrapper);
	}

	@Override
	public Boolean DeleteAsset(Integer id) {
		// TODO Auto-generated method stub
		Asset asset = this.getById(id);
	
		if (asset == null) {
			return false;
		} else {
			asset.setDelflag(1);
			return this.updateById(asset);
		}
	}

	@Override
	public Asset SelectAsset(Integer id) {
		// TODO Auto-generated method stub
		QueryWrapper<Asset> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id);
		queryWrapper.ne("state", 0);
		return this.getOne(queryWrapper);
	}

	@Override
	public IPage<Asset> ListAssetWithCreatorAndType(Integer id, Integer type, Integer page, Integer count,
			Boolean withoutBan) {
		QueryWrapper<Asset> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("created_by", id);
		if (type != 0) {
			queryWrapper.eq("type", type);
		}
		queryWrapper.ne("state", 0);

		IPage<Asset> assetPage = new Page<>(page, count);
		return page(assetPage, queryWrapper);
	}

}
